package com.qnbc.mystery.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qnbc.mystery.core.annotation.Log;
import com.qnbc.mystery.core.controller.BaseController;
import com.qnbc.mystery.core.domain.AjaxResult;
import com.qnbc.mystery.core.enums.BusinessType;
import com.qnbc.mystery.core.page.TableDataInfo;
import com.qnbc.mystery.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qnbc.mystery.domain.BoxInfo;
import com.qnbc.mystery.service.IBoxInfoService;

/**
 * 奖盒管理
 *
 * @author guzj
 * @date 2024-01-30
 */
@Api("奖盒管理")
@RestController
@RequestMapping("/boxInfo")
public class BoxInfoController extends BaseController {
    @Autowired
    private IBoxInfoService boxInfoService;

    /**
     * 查询奖盒管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BoxInfo boxInfo) {
        startPage();
        List<BoxInfo> list = boxInfoService.selectBoxInfoList(boxInfo);
        return getDataTable(list);
    }

    /**
     * 上一箱
     */
    @GetMapping("/shangBox")
    public AjaxResult shangBox(BoxInfo boxInfo) {
        BoxInfo boxInfoquery = new BoxInfo();
        List<BoxInfo> list = boxInfoService.selectBoxInfoList(boxInfo);
        for (int i = 0; i < list.size(); i++) {
            BoxInfo boxInfo1 = list.get(i);
            if (Objects.equals(boxInfo1.getId(), boxInfo.getId())) {
                if (i - 1 < 0) {
                    boxInfoquery = list.get(list.size()-1);
                } else {
                    boxInfoquery = list.get(i - 1);
                }
            }
        }
        BoxInfo boxInfo1 = boxInfoService.selectBoxInfoById(boxInfoquery.getId());
        return AjaxResult.success(boxInfo1);
    }

    /**
     * 下一箱
     */
    @GetMapping("/xiaBox")
    public AjaxResult xiaBox(BoxInfo boxInfo) {
        BoxInfo boxInfoquery = new BoxInfo();
        List<BoxInfo> list = boxInfoService.selectBoxInfoList(boxInfo);
        for (int i = 0; i < list.size(); i++) {
            BoxInfo boxInfo1 = list.get(i);
            if (Objects.equals(boxInfo1.getId(), boxInfo.getId())) {
                if (i + 1 >= list.size()) {
                    boxInfoquery = list.get(0);
                } else {
                    boxInfoquery = list.get(i + 1);
                }
            }
        }
        BoxInfo boxInfo1 = boxInfoService.selectBoxInfoById(boxInfoquery.getId());

        return AjaxResult.success(boxInfo1);
    }


    /**
     * 更多魔力赏  5 个
     */
    @ApiOperation("更多魔力赏 5 个")
    @GetMapping("/listMls")
    public AjaxResult listMls(BoxInfo boxInfo) {
        PageHelper.startPage(boxInfo.getPageNum(), boxInfo.getPageSize());
        boxInfo.setBoxType(3);
        List<BoxInfo> list = boxInfoService.selectBoxInfoList(boxInfo);
        PageInfo pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }


    /**
     * 导出奖盒管理列表
     */
    @Log(title = "奖盒管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BoxInfo boxInfo) {
        List<BoxInfo> list = boxInfoService.selectBoxInfoList(boxInfo);
        ExcelUtil<BoxInfo> util = new ExcelUtil<BoxInfo>(BoxInfo.class);
        util.exportExcel(response, list, "奖盒管理数据");
    }

    /**
     * 获取奖盒管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(boxInfoService.selectBoxInfoById(id));
    }

    /**
     * 新增奖盒管理
     */
    @Log(title = "奖盒管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BoxInfo boxInfo) {
        return toAjax(boxInfoService.insertBoxInfo(boxInfo));
    }

    /**
     * 修改奖盒管理
     */
    @Log(title = "奖盒管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BoxInfo boxInfo) {
        return toAjax(boxInfoService.updateBoxInfo(boxInfo));
    }

    /**
     * 删除奖盒管理
     */
    @Log(title = "奖盒管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(boxInfoService.deleteBoxInfoByIds(ids));
    }
}
